package edu.hawaii.wattwaiter.wattdepotcommands;

import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

/**
 * Tests the CarbonEmittedCollector to ensure that
 * it is working properly.
 * 
 * @author David Mau
 */
public class TestCarbonEmittedCollector {

  /**
   * Tests the normal operation of the CarbonEmittedCollector.
   */
  @Test
  public void testCarbonEmittedCollector() {
    // Create a new CarbonEmittedCollector object.
    CarbonEmittedCollector test = new CarbonEmittedCollector(null);
    // Turn on console debug mode.
    //test.debugModeOn();
    // Test to see if the default server was used.
    assertSame( "Testing to see if the default client was used.", test.client.getWattDepotUri(),
        test.defaultServer );
    
    // Go through the allZero list to see if all of the entries are 0.0.
    for ( Double entry : test.allZeroList ) {
      assertEquals( "Checking item in the allZeroList.", 0.0, entry, 0);
    }
    
    // Get the results from the Class.
    List<Double> results = test.getList( "2009-11-01" );
    
    // Ready an ArrayList of known results.
    ArrayList<Double> known = new ArrayList<Double>();
    known.add( 802164.0 );
    known.add( 798676.0 );
    known.add( 796932.0 );
    known.add( 798676.0 );
    known.add( 801292.0 );
    known.add( 823092.0 );
    known.add( 860588.0 );
    known.add( 894596.0 );
    known.add( 928604.0 );
    known.add( 967844.0 );
    known.add( 1000567.0 );
    known.add( 1039425.0 );
    known.add( 1077875.0 );
    known.add( 1093650.0 );
    known.add( 1090950.0 );
    known.add( 1086450.0 );
    known.add( 1087350.0 );
    known.add( 1087350.0 );
    known.add( 1084525.0 );
    known.add( 1076325.0 );
    known.add( 1030705.0 );
    known.add( 959996.0 );
    known.add( 892852.0 );
    known.add( 828324.0 );
    
    assertEquals( "Checking that results and known data have same size Lists.", 
        results.size(), known.size() );
    
    // Ensure that all of the items in the list match
    for ( int counter = 0; counter < 24; counter++ ) {
      assertEquals( "Checking items in output list.", 
          results.get( counter ), known.get( counter ), 0 );
    }
    
    // Try to use another server.
    test = new CarbonEmittedCollector("http://www.google.com/");
    // Turn on console debug mode.
    //test.debugModeOn();
    // Test to see if the specified server was used.
    assertSame( "Testing to see if the given client was used.", test.client.getWattDepotUri(),
        "http://www.google.com/" );
  }
  
  /**
   * A test to see if CarbonEmittedCollector can handle being given a bad server 
   * in the correct fashion.
   */
  @Test
  public void testBadServer() {
    // Create a new CarbonEmittedCollector object.
    CarbonEmittedCollector test = new CarbonEmittedCollector("http://www.google.com/");
    // Turn on console debug mode.
    //test.debugModeOn();

    // Get the results from the Class.
    List<Double> results = test.getList( "2009-11-01" );
    
    // Go through the results list to see if all of the entries are 0.0.
    for ( Double entry : results ) {
      assertEquals( "Checking for all 0.0s in the results for giving a bad server.",
          0.0, entry, 0);
    }

  }
  
  /**
   * A test to see if CarbonEmittedCollector can handle being given a bad date
   * string that is larger than what is expected.
   */
  @Test
  public void testTooLongDate() {
    // Create a new CarbonEmittedCollector object.
    CarbonEmittedCollector test = new CarbonEmittedCollector(null);
    // Turn on console debug mode.
    //test.debugModeOn();
    // Get the results from the Class.
    List<Double> results = test.getList( "JUST WAY TOO LONG A STRING" );
    
    // Go through the results list to see if all of the entries are 0.0.
    for ( Double entry : results ) {
      assertEquals( "Checking for all 0.0s in the results for giving a string that was too long" +
          " for the date.", 0.0, entry, 0);
    }
    
  }
  
  /**
   * A test to see if CarbonEmittedCollector can handle being given a bad date
   * string that is not convertible into a time stamp.
   */
  @Test
  public void testBadDate() {
    // Create a new CarbonEmittedCollector object.
    CarbonEmittedCollector test = new CarbonEmittedCollector(null);
    // Turn on console debug mode.
    //test.debugModeOn();
    // Get the results from the Class.
    List<Double> results = test.getList( "TenLetters" );
    
    // Go through the results list to see if all of the entries are 0.0.
    for ( Double entry : results ) {
      assertEquals( "Checking for all 0.0s in the results for giving a string that is not" +
          " a date.", 0.0, entry, 0);
    }
    
  }
  
  /**
   * A test to see if CarbonEmittedCollector can handle being given a bad date
   * string that will give no results.
   */
  @Test
  public void testBadDateForResults() {
    // Create a new CarbonEmittedCollector object.
    CarbonEmittedCollector test = new CarbonEmittedCollector(null);
    // Turn on console debug mode.
    //test.debugModeOn();
    // Get the results from the Class.
    List<Double> results = test.getList( "1900-01-01" );
    
    // Go through the results list to see if all of the entries are 0.0.
    for ( Double entry : results ) {
      assertEquals( "Checking for all 0.0s in the results for giving a bad date that" +
          " won't have results.", 0.0, entry, 0);
    }
    
  }
  
  /**
   * A test to see if CarbonEmittedCollector can handle being given a null date
   * string.
   */
  @Test
  public void testNullDateHandling() {
    // Create a new CarbonEmittedCollector object.
    CarbonEmittedCollector test = new CarbonEmittedCollector(null);
    // Turn on console debug mode.
    //test.debugModeOn();
    // Get the results from the Class.
    List<Double> results = test.getList( null );
    
    // Go through the results list to see if all of the entries are 0.0.
    for ( Double entry : results ) {
      assertEquals( "Checking for all 0.0s in the results for giving a null date.", 
          0.0, entry, 0);
    }
    
  }
  
  /**
   * A test to ensure that the program handles cross-month days correctly.
   * Before the bug was fixed, the program couldn't handle it.
   */
  @Test
  public void testBorderOfMonths() {
    // Create a new CarbonEmittedCollector object.
    CarbonEmittedCollector test = new CarbonEmittedCollector(null);
    // Turn on console debug mode.
    //test.debugModeOn();
    
    // Get the results from the Class.
    List<Double> results = test.getList( "2009-12-07" );
    
    // Check the contents of the results.
    for ( Double result : results ) {
      assertNotSame( "Checking list contents to ensure cross-month bug removal.", 
          result, 0.0 );
    }
  }
}
